<template>
  <el-dialog title="分配权限" :visible="isShow" @close="closeEvent">
    <el-tree
      ref="tree"
      :data="treeData"
      show-checkbox
      :props="{ label: 'name' }"
      node-key="id"
      :default-checked-keys="defaultKey"
      :check-strictly="true"
    />

    <template #footer>
      <div style="text-align: center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permissions'
import { changeData } from '@/utils/index.js'
import { sysRoleMoreInfo, sysRoleAssignPrem } from '@/api/setting'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: [],
      defaultKey: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取所有权限点
    async getData() {
      const res = await sysPermission()
      console.log(res)
      this.treeData = changeData(res.data, '0')
    },
    // 关闭弹窗
    closeEvent() {
      this.$emit('update:isShow', false)
      this.$refs.tree.setCheckedKeys([])
    },
    // 根据角色id获取角色详细信息(包括权限)
    async getPermission(id) {
      this.id = id
      const res = await sysRoleMoreInfo(id)
      console.log(res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 给角色分配权限
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('分配权限成功!')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
